package com.yh.hljapp.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.Snackbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.yh.hljapp.AppContext;
import com.yh.hljapp.R;
import com.yh.hljapp.base.BaseFragment;
import com.yh.hljapp.common.StringUtils;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * =========================
 * 版权所有，违法必究
 * 作者：zhanghang
 * <p>
 * 工程：hlj
 * <p>
 * 文件名：Ip_port_Fragment
 * <p>
 * 创建时间：2017.09.14.9:07
 */

public class Ip_port_Fragment extends BaseFragment {

    @InjectView(R.id.et_ip)
    EditText etIp;
    @InjectView(R.id.et_port)
    EditText etPort;
    @InjectView(R.id.btn_save)
    Button btn_save;

    private String mTvIps = "";
    private String mTvPorts = "";
    private Socket mSocket;

    public Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            hideWaitDialog();
            if (msg.what == 0) {
                Bundle bundle = msg.getData();
                Snackbar.make(mActivity.getWindow().getDecorView(), bundle.getString("msg"), Snackbar.LENGTH_SHORT).show();

                AppContext.getInstance().setProperty("rm_ip", bundle.getString("ip"));
                AppContext.getInstance().setProperty("rm_port", bundle.getString("port"));

            } else if (msg.what == 1) {
                Bundle bundle = msg.getData();
                Snackbar.make(mActivity.getWindow().getDecorView(), bundle.getString("msg"), Snackbar.LENGTH_SHORT).show();
            }
        }

    };


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = View.inflate(mActivity, R.layout.fragment_ip_port, null);
        ButterKnife.inject(this, view);
        initView(view);
        initData();
        return view;
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        btn_save.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.btn_save:
                mTvIps = etIp.getText().toString();
                mTvPorts = etPort.getText().toString();
                if (!StringUtils.isEmpty(mTvIps) && !StringUtils.isEmpty(mTvPorts)) {
                    boolean ipok = StringUtils.ipCheck(mTvIps);
                    if (ipok) {

                        startSocket(mTvIps, mTvPorts);

                    } else {
                        Snackbar.make(mActivity.getWindow().getDecorView(), "Ip不合法！", Snackbar.LENGTH_SHORT).show();
                    }
                } else {
                    Snackbar.make(mActivity.getWindow().getDecorView(), "必填信息不能为空！", Snackbar.LENGTH_SHORT).show();
                }
                break;
        }
    }


    @Override
    public void initData() {
        super.initData();
        etIp.setText(AppContext.getInstance().getProperty("rm_ip"));
        etPort.setText(AppContext.getInstance().getProperty("rm_port"));

    }

    private void startSocket(String tvIps, String tvPorts) {

        showWaitDialog("链接服务器中...");
        int port = Integer.parseInt(tvPorts);
        new MyThread(tvIps, port).start();

    }

    class MyThread extends Thread {
        public String ip;
        public int port;

        public MyThread(String ip, int port) {
            this.ip = ip;
            this.port = port;
        }

        @Override
        public void run() {
            // 定义消息
            Message msg = new Message();
            Bundle bundle = new Bundle();
            bundle.clear();
            try {
                // 连接服务器 并设置连接超时为5秒
                mSocket = new Socket();
                mSocket.connect(new InetSocketAddress(ip, port), 5000);
                mSocket.close();
                msg.what = 0;
                bundle.putString("msg", "服务器连接成功");
                bundle.putString("ip", ip);
                bundle.putString("port", port + "");
                msg.setData(bundle);
                // 发送消息 修改UI线程中的组件
                myHandler.sendMessage(msg);
            } catch (SocketTimeoutException aa) {
                // 连接超时 在UI界面显示消息
                bundle.putString("msg", "服务器连接失败！请检查ip和端口");
                msg.what = 1;
                msg.setData(bundle);
                // 发送消息 修改UI线程中的组件
                myHandler.sendMessage(msg);
            } catch (IOException e) {
                bundle.putString("msg", "服务器连接失败！请检查ip和端口");
                msg.what = 1;
                msg.setData(bundle);
                // 发送消息 修改UI线程中的组件
                myHandler.sendMessage(msg);
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
